import requests
from bs4 import BeautifulSoup
import openpyxl


# 下厨房美食

def send_requst():
    url = "https://www.xiachufang.com/explore/"
    headers = {
        "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36',
    }
    resp = requests.get(url=url, headers=headers)
    resp.encoding = 'utf-8'
    return resp.text


def parse_html(html):
    # 解析数据
    count = 0
    bs = BeautifulSoup(html, 'lxml')
    lst_name = bs.find_all('p', class_='name')
    lst_category = bs.find_all('p', class_='ing ellipsis')
    lst = []
    for i in range(len(lst_name)):
        count += 1
        food_url = 'https://www.xiachufang.com' + lst_name[i].find('a')['href']
        lst.append([count, lst_name[i].text[18:-14], lst_category[i].text[1:-1], food_url])
    save(lst)


def save(lst):
    wb = openpyxl.Workbook()
    sheet = wb.active
    for row in lst:
        sheet.append(row)
    wb.save("下厨房美食.xlsx")


def start():
    result = send_requst()
    parse_html(result)


if __name__ == '__main__':
    start()
